// 胡子的消息字段
P =
    {
        GS_Login: 1,	//登录
        GS_StatusChange: 11,  //客户端状态变更
        GS_UserJoin: 101,
        GS_UserLeave: 102,
        GS_UserDisconnect: 103,	//玩家临时掉线，真正掉线采用 	GS_UserLeave
        GS_GameOver: 105, 	//服务器宣布游戏结束
        GS_GameResult: 106,	//游戏战绩显示.
        GS_CardDeal: 107,	//发牌
        GS_GameStart: 109,	//开始游戏，之后发牌
        GS_GameTurn_Out: 110, //到谁出牌了，开始读秒
        GS_GameTurn_In: 111, //请客户端选择是否进牌
        GS_BroadcastAction: 112,  // 广播
        GS_RoomInfo: 113,  // 断线重连之后，获取房间ID
        GS_Create_Room: 114, 	//创建一个房间
        GS_Chat: 115, //聊天广播
        GS_Vote: 116, //投票广播
        GS_Marquee: 117, // 跑马灯
        GS_RoomResult: 118,  //房间结算
        GS_UserKick: 119, //号被顶
        GS_Niao_Status: 120,
        GS_Pls_Disconnect: 121,
        GS_Jushou_Status: 122, //广播三个玩家的举手状态，在每一轮开始的时候广播
        GS_Baojing_Status: 123,
        GS_Fanxing_Status: 124,
        GS_Wangdiao_status: 125,//等待玩家选王调
        GS_ReadyStatus: 126, //所有玩家的准备状态

        //牛牛
        GS_Login_NiuNiu: 501,
        GS_UserJoin_NiuNiu: 601,
        GS_GameStart_NiuNiu: 609,
        GS_Vote_NiuNiu: 616,
        GS_RoomResult_NiuNiu: 618,
        GS_NiuniuOver: 130, 	//服务器宣布游戏结束
        GS_GameChipIn: 200, 	//下注
        GS_GameShowHand: 201,	//翻牌，亮牌
        GS_ReadyNotify: 202,	//int变量的status变化
        GS_Qiangzuang: 203,	//抢庄。 -1 可以抢，0不能抢 1：抢 2:放弃
        GS_Preview: 204,
        GS_AutoAction: 206, //倒计时
        GS_Sitdown: 207,
        GS_LastResult: 208,

        GS_NetWorkClose: 10001,//网络断开，

        GS_TishiPai_List: 98, //提示牌列表
        GS_TingPai_List: 99  //提示听牌列表
    };

// Utils Function.
var setUIImageFrameByName = function (sprite, name, plist) {
    for (var i = 0; i < 3; i++) {
        var frame = cc.spriteFrameCache.getSpriteFrame(name);
        if (!frame) {
            cc.log('setUIImageFrameByName = ' + plist + ', name = ' + name);
            // cc.spriteFrameCache.addSpriteFrames(huzi.res[plist + '.plist']);
            switch (plist) {
                case 'card':
                    cc.spriteFrameCache.addSpriteFrames(huzi.res.card_plist);
                    break;
                case 'card_common':
                    cc.spriteFrameCache.addSpriteFrames(huzi.res.card_common_plist);
                    break;
            }
        } else {
            sprite.loadTexture(name, ccui.Widget.PLIST_TEXTURE);
            return;
        }
    }
    if (!cc.sys.isNative)
        throw new Error('getSpriteFrame(' + name + ') == null');
    else
        showAlert('getSpriteFrame(' + name + ') == null');
};

var bgArr = [
    ['table_back_0', 'table_backbig_0', 'table_paibei_0', 'table_third_0'],
    ['table_back_1', 'table_backbig_1', 'table_paibei_1', 'table_third_1'],
    ['table_back_2', 'table_backbig_2', 'table_paibei_2', 'table_third_2'],
    ['table_back_3', 'table_backbig_3', 'table_paibei_3', 'table_third_3'],
];


if (duplicateSprite == undefined) {
    var duplicateSprite = function (sprite, isCopyUserdata) {
        var newSprite = null;
        if (sprite instanceof cc.Sprite) {
            newSprite = new cc.Sprite(sprite.getSpriteFrame());
            newSprite.setBlendFunc(sprite.getBlendFunc());
        }
        else if (sprite instanceof ccui.CheckBox) {
            if (cc.sys.isNative)
                newSprite = sprite.clone();
            else {
                newSprite = new ccui.CheckBox();
                newSprite._copySpecialProperties(sprite);
            }
        }
        else if (sprite instanceof ccui.Text) {
            newSprite = new ccui.Text();
            newSprite.setFontName(newSprite.getFontName());
            newSprite.setTextColor(sprite.getTextColor());
            newSprite.setFontSize(sprite.getFontSize());
            newSprite.setTextHorizontalAlignment(sprite.getTextHorizontalAlignment());
            newSprite.setTextVerticalAlignment(sprite.getTextVerticalAlignment());
        } else if (sprite instanceof ccui.TextBMFont) {
            newSprite = sprite.clone();
            // return;
        } else if (sprite instanceof ccui.ImageView) {
            newSprite = sprite.clone();
            return newSprite;
        } else if (sprite instanceof ccui.TextAtlas) {
            newSprite = sprite.clone();
            newSprite.setString(sprite.getString());
            return newSprite;
        } else if (sprite instanceof cc.Layer) {
            newSprite = new cc.Layer();
        } else if (sprite instanceof cc.Node) {
            newSprite = new cc.Node();
        }
        if (!sprite) {
            alertError('duplicateSprite', 'sprite:' + sprite, 'sprite is undefined');
            network.disconnect();
            return;
        }
        if (!newSprite) {
            alertError('duplicateSprite', 'sprite:' + sprite.getName(), 'newSprite is undefined');
            network.disconnect();
            return;
        }

        newSprite.setAnchorPoint(sprite.getAnchorPoint());
        newSprite.setScale(sprite.getScaleX(), sprite.getScaleY());
        newSprite.setPosition(sprite.getPosition());

        newSprite.setContentSize(sprite.getContentSize());
        newSprite.setColor(sprite.getColor());
        newSprite.setOpacity(sprite.getOpacity());
        newSprite.setVisible(sprite.isVisible());
        newSprite.setName(sprite.getName());
        if (sprite instanceof cc.Sprite) {
            var children = sprite.getChildren();
            for (var i = 0; i < children.length; i++) {
                var node = duplicateSprite(children[i], true);
                newSprite.addChild(node);
            }
        }
        if (isCopyUserdata)
            newSprite.setUserData(_.clone(sprite.getUserData()));
        return newSprite;
    };
}

if (duplicateLayout == undefined) {
    var duplicateLayout = function (layout) {
        var newLayout = null;
        if (layout instanceof cc.Sprite)
            newLayout = duplicateSprite(layout);
        else if (layout instanceof ccui.Layout) {
            newLayout = layout.clone();
        }
        else if (layout instanceof cc.Node) {
            if (layout.getName() == 'Layer') {
                newLayout = new cc.LayerColor(layout.getColor(), layout.getContentSize().width, layout.getContentSize().height);
                // newLayout.setContentSize();
            } else {
                newLayout = new cc.Node();
            }
        }
        if (newLayout === null) {
            alertError('duplicateLayout', 'layout:' + layout.getName(), 'newLayout is undefined');
            network.disconnect();
            return;
        }
        newLayout.setPosition(layout.getPosition());
        newLayout.setScaleX(layout.getScaleX());
        newLayout.setScaleY(layout.getScaleY());
        newLayout.setAnchorPoint(layout.getAnchorPoint());
        newLayout.setOpacity(255);
        newLayout.setVisible(true);
        var children = layout.getChildren();
        for (var i = 0; i < children.length; i++) {
            var node = duplicateSprite(children[i], true);
            newLayout.addChild(node);
        }
        newLayout.setName(layout.getName());
        return newLayout;
    };
}

ERR_CODE =
    {
        VERSION_ERR: 10,
    };

/*mAction.js*/


TIANHU = 0x1; 	//天胡1
DIHU = 0x2; 	//地胡2
TI = 0x4;		//提4
WEI = 0x8;	    //偎8
CHOUWEI = 0x10; //臭畏16
HU = 0x20;		//胡32
PAO = 0x40;		//跑64
PENG = 0x80;	//碰128
CHI = 0x100;	//吃256
GUO = 0x10000;  //65536

var mAction = {
    acts: {
        'chi': CHI,
        'peng': PENG,
        'ti': TI,
        'pao': PAO,
        'hu': HU,
        'tianhu': TIANHU,
        'wei': WEI,
        'chouwei': CHOUWEI,
        'none': GUO
    },

    combos: {},
    hx: {},
    outCards: {},
    leftCardCount: 0,

    init: function () {
    },

    initGame: function () {
        mAction.combos = {
            0: [],
            1: [],
            2: [],
            3: []
        };

        mAction.hx = {
            0: 0,
            1: 0,
            2: 0,
            3: 0
        };
        mAction.showHx = {
            0: 0,
            1: 0,
            2: 0,
            3: 0
        };
        mAction.outCards = {
            0: [],
            1: [],
            2: [],
            3: []
        };

        mAction.leftCardCount = 80;
    },

    getActionString: function (act) {
        var str = '';
        var acts = mAction.acts;
        for (var key in acts) {
            if (acts[key] == act) {
                return key;
            }
        }
        return str;
    },

    getActionByString: function (actStr) {
        var act = mAction.acts[actStr];
        return act;
    },

    checkOpen: function (target, typ) {
        //如果是录像回放不判断
        if (999999 == target) {
            return true;
        }
        if ((typ & target) != 0) {
            return true;
        }
        return false;
    },

    getBTAct: function (index, actList) {
        //if(index == 3)return true;
        for (var i = 0; i < actList.length; i++) {
            var act = actList[i];
            if (index == 1 && act == CHI) {
                return CHI;
            } else if (index == 2 && act == PENG) {
                return PENG;
            } else if (index == 0 && act == HU) {
                return HU;
            } else if (index == 3 && act == GUO) {
                return GUO;
            } else if (index == 3 && act == WEI) {
                return WEI;
            } else if (index == 3 && act == PAO) {
                return PAO;
            }
        }
        return 0;
    },

    getActList: function (target, card) {
        var actList = [];
        var list = this.isCanAct(target, CHI, card);
        if (list.length > 0) actList.push(CHI);

        list = this.isCanAct(target, PENG, card);
        if (list.length > 0) actList.push(PENG);

        return actList;
    },

    isCanAct: function (target, typ, card) {
        var retList = [];
        card = parseInt(card);

        if (target != null && this.checkOpen(target, typ)) {
            switch (typ) {
                case PENG:
                    retList = this.isHaveSame(card, 2);
                    break;
                case CHI:
                    retList = this.isHaveChi(card);
                    break;
            }
        }
        return retList;
    },

    isCanWei: function (target) {
        if (this.checkOpen(target, WEI) == true || this.checkOpen(target, CHOUWEI) == true)
            return true;
        return false;
    },

    isCanPaoTi: function (target, card, isMo, isSelf) {
        var paoInfo = {};
        var retList = [];
        var isOpen = false;
        paoInfo.act = null;

        card = parseInt(card);
        if (target != null && (this.checkOpen(target, PAO) || this.checkOpen(target, TI))) {
            retList = this.isHaveSame(card, 3);
            if (retList.length > 0) {
                if (isMo == true && isSelf == true) {
                    //TI  玩家手中有一坎，摸起相同的一只牌后，必须四只放置于桌面
                    paoInfo.act = TI;
                    paoInfo.old = mCard.comboTypes.kan;
                    paoInfo.newt = mCard.comboTypes.ti;
                } else if (isSelf == false) {
                    //PAO 玩家手中有一坎牌，当其他玩家打出或摸出一只相同的牌，玩家必须跑牌
                    paoInfo.act = PAO;
                    paoInfo.old = mCard.comboTypes.kan;
                    paoInfo.newt = mCard.comboTypes.pao;
                }
            } else {
                for (var i = 0; i < mAction.combos[0].length; i++) {
                    var comboInfo = mAction.combos[0][i];
                    var cardInCombo = comboInfo.cards[0];
                    if (card == cardInCombo) {
                        if (comboInfo.typ == mCard.comboTypes.wei || comboInfo.typ == mCard.comboTypes.chouwei) {
                            if (isSelf == true) {
                                //TI 玩家桌面已有偎的牌，摸起相同的一只牌后，必须和原来的三只一起放置于桌面
                                paoInfo.act = TI;
                                retList = comboInfo.cards;
                                isOpen = true;
                                paoInfo.old = comboInfo.typ;
                                paoInfo.newt = mCard.comboTypes.ti;
                                break;
                            } else {
                                //PAO 玩家桌面已有偎的牌，当其他玩家打出或摸出相同的牌后，必须跑牌
                                paoInfo.act = PAO;
                                retList = comboInfo.cards;
                                isOpen = true;
                                paoInfo.old = comboInfo.typ;
                                paoInfo.newt = mCard.comboTypes.pao;
                                break;
                            }
                        } else if (comboInfo.typ == mCard.comboTypes.peng && isMo == true) {
                            //PAO 玩家桌面已有碰的牌，当自己或其他玩家摸出相同的牌后，必须跑牌
                            paoInfo.act = PAO;
                            retList = comboInfo.cards;
                            isOpen = true;
                            paoInfo.old = comboInfo.typ;
                            paoInfo.newt = mCard.comboTypes.pao;
                            break;
                        }
                    }
                }
            }
        }
        paoInfo.cards = retList;
        paoInfo.isOpen = isOpen;
        return paoInfo;
    },

    isHaveSame: function (card, count, cards) {
        card = parseInt(card);
        cards = cards || mRoom['CardList'];
        var sameList = [];
        for (var i = 0; i < cards.length; i++) {
            if (cards[i] == card) {
                sameList.push(cards[i]);
            }
        }

        if (sameList.length == count) {
            return sameList;
        }
        return [];
    },

    isHaveChi: function (card, checkCards) {
        card = parseInt(card);
        var chiList = [];

        var cards = checkCards || mRoom['CardList'];
        var newCardList = [];
        for (var i = 0; i < cards.length; i++) {
            newCardList.push(cards[i]);
        }

        var retList = [];
        mCard.searchSameCard(retList, newCardList, 3);
        if (checkCards == null)
            newCardList.push(card);

        var count = mCard.getCardCount(newCardList, card);

        this.isHave2710Card(chiList, card, newCardList);
        this.isHave1510Card(chiList, card, newCardList);
        this.isHave123Card(chiList, card, newCardList);
        this.isHaveJiaoCard(chiList, card, newCardList);

        if (count > 1) {                  // 2级
            for (var i = chiList.length - 1; i >= 0; i--) {
                var leftCards = mCard.copyCards(newCardList);
                var chiInfo = chiList[i];
                for (var x = 0; x < chiInfo.length; x++) {
                    removeObjArray(leftCards, chiInfo[x]);
                }

                var leftCount = mCard.getCardCount(leftCards, card);
                if (leftCount == 0) {  // 没有能组合的吃卡了
                    continue;
                }

                var subList = [];
                this.isHave2710Card(subList, card, leftCards);
                this.isHave1510Card(subList, card, leftCards);
                this.isHave123Card(subList, card, leftCards);
                this.isHaveJiaoCard(subList, card, leftCards);

                if (count > 2) {         // 3级
                    for (var j = subList.length - 1; j >= 0; j--) {
                        var endCards = mCard.copyCards(leftCards);
                        var subInfo = subList[j];
                        for (var x = 0; x < subInfo.length; x++) {
                            removeObjArray(endCards, subInfo[x]);
                        }

                        var leftCount2 = mCard.getCardCount(endCards, card);
                        if (leftCount2 == 0) {  // 没有能组合的吃卡了
                            continue;
                        }

                        var endList = [];
                        this.isHave2710Card(endList, card, endCards);
                        this.isHave1510Card(endList, card, endCards);
                        this.isHave123Card(endList, card, endCards);
                        this.isHaveJiaoCard(endList, card, endCards);

                        if (endList.length > 0) {
                            subInfo.push(endList);
                        } else {
                            subList.splice(j, 1);
                        }
                    }
                }

                if (subList.length > 0) {
                    chiInfo.push(subList);
                } else {
                    chiList.splice(i, 1);
                }
            }
        }

        return chiList;
    },

    isHaveBi: function (card) {
        var leftCards = mCard.copyCards(mRoom['CardList']);
        var comboList = mCard.copyCards(comboListSrc);
        leftCards.push(card);

        comboList.push(cards);
        for (var i = 0; i < comboList.length; i++) {
            var combo = comboList[i];
            for (var j = 0; j < combo.length; j++) {
                removeObjArray(leftCards, combo[j]);
            }
        }

        var rowIndex = comboList.length;
        var children = this.nChiList.getChildren();
        for (var i = 0; i < children.length; i++) {
            var child = children[i];
            if (child.getTag() >= rowIndex) {
                child.removeFromParent();
            }
        }

        var py = rowIndex * 240 * -1;
        var chiList = mAction.isHaveChi(card, leftCards);
        for (var i = 0; i < chiList.length; i++) {
            var cardSelect = new CardSelect();
            this.nChiList.addChild(cardSelect);
            cardSelect.setData(this.showNo, chiList[i], this, comboList);
            cardSelect.setPosition(i * 90, py);
            cardSelect.setTag(rowIndex);
        }

        if (chiList.length > 0)
            return true;
        return false;
    },

    checkNeeds: function (needs, card, cards, isDui) {
        card = parseInt(card);
        var retList = [];
        var isHave = [false, false, false];
        if (isDui) {
            isHave = [false, false];
        }

        var isInCards = false;
        if (cards != null) {
            isInCards = true;
        }
        cards = cards || mRoom['CardList'];
        for (var i = 0; i < cards.length; i++) {
            var cardNumber = cards[i];

            for (var j = 0; j < needs.length; j++) {
                if (cardNumber == needs[j] && isHave[j] != true) {
                    isHave[j] = true;
                    break;
                }
            }
        }

        if (isInCards == false) {
            for (var j = 0; j < needs.length; j++) {
                if (card == needs[j] && isHave[j] != true) {
                    isHave[j] = true;
                    break;
                }
            }
        }

        var allHave = true;
        for (var j = 0; j < isHave.length; j++) {
            if (isHave[j] != true) {
                allHave = false;
                break;
            }
        }

        if (allHave == true) {
            retList = needs;
        }
        return retList;
    },

    isHaveDuiCard: function (chiList, card, cards) {
        card = parseInt(card);
        var needs = [card, card];
        var retList = this.checkNeeds(needs, card, cards, true);
        if (retList.length > 0) {
            chiList.push(retList);
        }
    },

    isHaveKanCard: function (chiList, card, cards) {
        card = parseInt(card);
        var needs = [card, card, card];
        var retList = this.checkNeeds(needs, card, cards);
        if (retList.length > 0) {
            chiList.push(retList);
        }
    },

    isHave2710Card: function (chiList, card, cards) {
        card = parseInt(card);
        if (mCard.is2710(card) == false) {
            return;
        }
        var needs = [2, 7, 10];
        if (card > 10) {
            needs = [12, 17, 20];
        }

        var retList = this.checkNeeds(needs, card, cards);
        if (retList.length > 0) {
            chiList.push(retList);
        }
    },

    isHave1510Card: function (chiList, card, cards) {
        if (mRoom.is1510 != true) {
            return;
        }

        card = parseInt(card);
        if (mCard.is1510(card) == false) {
            return;
        }
        var needs = [1, 5, 10];
        if (card > 10) {
            needs = [11, 15, 20];
        }

        var retList = this.checkNeeds(needs, card, cards);
        if (retList.length > 0) {
            chiList.push(retList);
        }
    },

    isHave123Card: function (chiList, card, cards) {
        card = parseInt(card);
        var need1 = [card, card + 1, card + 2];
        var need2 = [card - 1, card, card + 1];
        var need3 = [card - 2, card - 1, card];

        if ((card > 10 && card < 19) || (card > 0 && card < 9)) {
            var retList = this.checkNeeds(need1, card, cards);
            if (retList.length > 0) {
                chiList.push(retList);
            }
        }

        if ((card > 11 && card < 20) || (card > 1 && card < 10)) {
            var retList = this.checkNeeds(need2, card, cards);
            if (retList.length > 0) {
                chiList.push(retList);
            }
        }

        if ((card > 12 && card < 21) || (card > 2 && card < 11)) {
            var retList = this.checkNeeds(need3, card, cards);
            if (retList.length > 0) {
                chiList.push(retList);
            }
        }
    },

    isHaveJiaoCard: function (chiList, card, cards) {
        card = parseInt(card);
        var need1, need2;
        if (card > 10) {
            need1 = [card, card, card - 10];
            need2 = [card, card - 10, card - 10];
        } else {
            need1 = [card, card, card + 10];
            need2 = [card, card + 10, card + 10];
        }

        var retList = this.checkNeeds(need1, card, cards);
        if (retList.length > 0) {
            chiList.push(retList);
        }
        var retList = this.checkNeeds(need2, card, cards);
        if (retList.length > 0) {
            chiList.push(retList);
        }
    },

    isHaveHu: function (card, isSelf, paoInfo) {
        if (paoInfo != null && paoInfo.act == PAO) {
            var huInfo1 = mAction.isHaveHuAct(card, isSelf, paoInfo);   //跑胡
            var huInfo2 = mAction.isHaveHuAct(card, isSelf, null);      //破跑胡

            var hx1 = 0;
            var hx2 = 0;
            if (huInfo1 != null) hx1 = huInfo1.hx;
            if (huInfo2 != null) hx2 = huInfo2.hx;

            if (hx1 >= hx2) {
                return huInfo1;
            } else {
                return huInfo2;
            }
        } else {
            return mAction.isHaveHuAct(card, isSelf, paoInfo);
        }
    },

    isHaveHuAct: function (card, isSelf, paoInfo) {
        var newCardList = [];
        var cards = mRoom['CardList'];
        for (var i = 0; i < cards.length; i++) {
            newCardList.push(cards[i]);
        }

        var isCardAdd = false;
        var openList = mCard.copyComboList(mAction.combos[0]);              //检查跑和提
        if (paoInfo != null && paoInfo.act != null) {
            if (paoInfo.isOpen == true) {
                for (var i = 0; i < openList.length; i++) {
                    var comb = openList[i];
                    var cardInCombo = comb.cards[0];
                    if (card == cardInCombo) {
                        if (comb.typ == mCard.comboTypes.peng || comb.typ == mCard.comboTypes.wei || comb.typ == mCard.comboTypes.chouwei) {
                            comb.cards = paoInfo.cards;
                            comb.typ = paoInfo.newt;
                            comb.cards.push(card);
                            break;
                        }
                    }
                }
            } else {                  //kan
                var comb = {};
                comb.cards = paoInfo.cards;
                comb.typ = paoInfo.newt;
                comb.cards.push(card);
                openList.push(comb);
                for (var i = 0; i < 3; i++) {
                    removeObjArray(newCardList, card);
                }
            }
            isCardAdd = true;
        }

        var cardCount = mCard.getCardCount(newCardList, card);
        if (isSelf == true && cardCount == 2)                    //偎牌
        {
            var comb = {};
            comb.cards = [card, card, card];
            comb.typ = mCard.comboTypes.wei;
            openList.push(comb);
            for (var i = 0; i < 2; i++) {
                removeObjArray(newCardList, card);
            }
            isCardAdd = true;
        }

        var kanRet = [];
        newCardList.sort(function (a, b) {
            return b - a;
        });
        mCard.searchSameCard(kanRet, newCardList, 3);

        if (isCardAdd == false) {                             //吃进
            newCardList.push(card);
        }

        newCardList.sort(function (a, b) {
            return b - a;
        });

        var retList = [];
        var okList = [];
        var huList = null;

        //this.checkLeftCards(huList, retList, newCardList, []);

        if (newCardList.length > 0) {
            this.checkLeftCards2(okList, retList, newCardList, []);
        } else {
            okList.push([]);
        }

        var deskHx = mCard.getHuXi(openList);
        var maxHx = 0;
        var kanHx = 0;
        var kanList = [];
        for (var i = 0; i < kanRet.length; i++) {
            if (kanRet[i][0] > 10) {
                kanHx += 6;
            } else {
                kanHx += 3;
            }
            var cObj = {};
            cObj.typ = mCard.comboTypes.kan;
            cObj.cards = kanRet[i];
            kanList.push(cObj);
        }
        for (var i = 0; i < okList.length; i++) {
            var hx = mCard.getHuXi(okList[i]) + deskHx + kanHx;
            if (hx >= 15 && hx > maxHx) {
                maxHx = hx;
                huList = okList[i];
            }
        }

        var huInfo = null;
        if (huList != null) {
            huInfo = {};
            huInfo.huList = huList;
            huInfo.kanList = kanList;
            huInfo.openList = openList;
            huInfo.hx = maxHx;
        }
        return huInfo;
    },

    //checkChiList:function(okList, retList, newCardList, card, typ){
    //    var chiList = [];
    //    switch (typ){
    //        case mCard.comboTypes.o2710:
    //            this.isHave2710Card(chiList, card, newCardList);
    //            break;
    //        case mCard.comboTypes.o234:
    //            this.isHave123Card(chiList, card, newCardList);
    //            break;
    //        case mCard.comboTypes.jiao:
    //            this.isHaveJiaoCard(chiList, card, newCardList);
    //            break;
    //    }
    //
    //    for(var i=0; i<chiList.length;i++){
    //        var combo = chiList[i];
    //        var retCopy = retList.concat();
    //        this.checkLeftCards(okList, retCopy, newCardList, combo, typ);
    //    }
    //},
    //
    //checkLeftCards:function(okList, retList, newCardList, combo, typ){
    //    if(combo.length > 0){
    //        for(var i=0;i<combo.length;i++){
    //            removeObjArray(newCardList, combo[i]);
    //        }
    //        var cObj = {};
    //        cObj.typ = typ;
    //        cObj.cards = combo;
    //        retList.push(cObj);
    //    }
    //
    //    if(newCardList.length > 2){
    //        var card = newCardList[0];
    //
    //        var checkList = this.isHaveSame(card, 3, newCardList);
    //        if(checkList.length > 0){
    //            var retCopy = retList.concat();
    //            this.checkLeftCards(okList, retCopy, newCardList, checkList, mCard.comboTypes.kan);
    //        }
    //
    //        this.checkChiList(okList, retList, newCardList, card, mCard.comboTypes.o2710);
    //        this.checkChiList(okList, retList, newCardList, card, mCard.comboTypes.o234);
    //        this.checkChiList(okList, retList, newCardList, card, mCard.comboTypes.jiao);
    //
    //    }else if(newCardList.length == 2){
    //        if(newCardList[0] == newCardList[1]){
    //            var card = newCardList[0];
    //            var combo = [card, card];
    //
    //            var cObj = {};
    //            cObj.typ = mCard.comboTypes.dui;
    //            cObj.cards = combo;
    //            retList.push(cObj);
    //            okList.push(retList);
    //        }
    //    }
    //    else{
    //        okList.push(retList);
    //    }
    //},

    checkChiList2: function (okList, retList, hasDui, newCardList, card, typ, myStack) {
        var chiList = [];
        switch (typ) {
            case mCard.comboTypes.peng:
                this.isHaveKanCard(chiList, card, newCardList);
                break;
            case mCard.comboTypes.o2710:
                this.isHave2710Card(chiList, card, newCardList);
                this.isHave1510Card(chiList, card, newCardList);
                break;
            case mCard.comboTypes.o234:
                this.isHave123Card(chiList, card, newCardList);
                break;
            case mCard.comboTypes.jiao:
                this.isHaveJiaoCard(chiList, card, newCardList);
                break;
            case mCard.comboTypes.dui:
                this.isHaveDuiCard(chiList, card, newCardList);
                break;
        }

        for (var i = 0; i < chiList.length; i++) {
            var combo = chiList[i];
            var retCopy = retList.concat();
            var cardsCopy = newCardList.concat();
            //this.checkLeftCards(okList, retCopy, newCardList, combo, typ);

            if (typ == mCard.comboTypes.o234 && (combo[0] == 1 || combo[0] == 11)) {
                typ = mCard.comboTypes.o123;
            }

            var stackObj = {};
            stackObj.okList = okList;
            stackObj.retList = retCopy;
            stackObj.newCardList = cardsCopy;
            stackObj.combo = combo;
            stackObj.typ = typ;
            stackObj.hasDui = hasDui;
            myStack.push(stackObj);
        }
    },

    checkLeftCards2: function (okList, retList, newCardList, combo, typ) {
        var myStack = [];
        var stackObj = {};
        stackObj.okList = okList;
        stackObj.retList = retList;
        stackObj.newCardList = newCardList;
        stackObj.combo = combo;
        stackObj.typ = typ;
        stackObj.hasDui = false;
        myStack.push(stackObj);
        var hasDui = false;

        while (true) {
            if (myStack.length > 0) {
                var stackObj = myStack.pop();
                okList = stackObj.okList;
                retList = stackObj.retList;
                newCardList = stackObj.newCardList;
                combo = stackObj.combo;
                typ = stackObj.typ;
                hasDui = stackObj.hasDui;
            } else {
                return;
            }

            if (combo.length > 0) {
                for (var i = 0; i < combo.length; i++) {
                    removeObjArray(newCardList, combo[i]);
                }
                var cObj = {};
                cObj.typ = typ;
                cObj.cards = combo;
                retList.push(cObj);

                if (typ == mCard.comboTypes.dui) {
                    hasDui = true;
                }
            }

            if (newCardList.length > 2) {
                var card = newCardList[0];

                this.checkChiList2(okList, retList, hasDui, newCardList, card, mCard.comboTypes.peng, myStack);
                this.checkChiList2(okList, retList, hasDui, newCardList, card, mCard.comboTypes.o2710, myStack);
                this.checkChiList2(okList, retList, hasDui, newCardList, card, mCard.comboTypes.o234, myStack);
                this.checkChiList2(okList, retList, hasDui, newCardList, card, mCard.comboTypes.jiao, myStack);
                //this.checkChiList2(okList, retList, hasDui, newCardList, card, mCard.comboTypes.jiao, myStack);
                if (hasDui == false) {
                    this.checkChiList2(okList, retList, hasDui, newCardList, card, mCard.comboTypes.dui, myStack);
                }
            } else if (newCardList.length == 2 && hasDui == false) {
                var card = newCardList[0];
                this.checkChiList2(okList, retList, hasDui, newCardList, card, mCard.comboTypes.dui, myStack);
            }
            else if (newCardList.length == 0) {
                okList.push(retList);
            }
        }
    }
};


/*mCard.js*/


var mCard = {
    init: function () {
    },

    comboTypes: {
        peng: 1,
        kan: 2,
        wei: 3,
        chouwei: 4,
        pao: 5,
        ti: 6,
        o123: 7,
        o2710: 8,
        o234: 9,
        dui: 10,
        jiao: 11,
        none: 12,
        chi: 13,
        hu: 14
    },

    comboTypes_ZN: {
        '碰': 1,
        '坎': 2,
        '偎': 3,
        '臭偎': 4,
        '跑': 5,
        '提': 6,
        '一二三': 7,
        '二七十': 8,
        '一句话': 9,
        '对': 10,
        '绞': 11,
        none: 12,
        chi: 13,
        hu: 14
    },

    //1-12
    comboImg: ['peng', 'kan', 'wei', 'wei', 'pao', 'ti', 'chi', 'chi', 'chi', 'jiang', 'chi', 'none'],

    combo_hx_Big: [3, 6, 6, 6, 9, 12, 6, 6, 0, 0, 0, 0, 0, 0],
    combo_hx_Small: [1, 3, 3, 3, 6, 9, 3, 3, 0, 0, 0, 0, 0, 0],


    copyCards: function (list) {
        var newCards = list.concat([]);
        return newCards;
    },

    copyComboList: function (list) {
        var newList = [];
        if (list != null) {
            for (var i = 0; i < list.length; i++) {
                var comboInfo = list[i];

                var newCombo = {};
                newCombo.typ = comboInfo.typ;
                newCombo.cards = comboInfo.cards.concat([]);
                newList.push(newCombo);
            }
        }
        return newList;
    },

    getComboType: function (combo, ziMo) {
        var typ = mCard.comboTypes.none;
        combo.sort(function (a, b) {
            return a - b;
        });

        if (combo.length == 4) {
            // var c1= parseInt(combo[0]);
            // var c2= parseInt(combo[1]);
            // var c3= parseInt(combo[2]);
            // var c4= parseInt(combo[3]);
            // if(ziMo == true)
            //     if(c1 == c2 && c1 == c3 && c1 == c4){
            //         typ = mCard.comboTypes.ti;
            //     }
            // else
            //     typ = mCard.comboTypes.pao;
        } else if (combo.length == 3) {
            var c1 = parseInt(combo[0]);
            var c2 = parseInt(combo[1]);
            var c3 = parseInt(combo[2]);

            if (c1 == c2 && c1 == c3) {
                if (ziMo == true)
                    typ = mCard.comboTypes.wei;
                else
                    typ = mCard.comboTypes.peng;
            } else if (c1 == c2 - 1 && c2 == c3 - 1) {
                if (c1 == 1 || c1 == 11) {
                    typ = mCard.comboTypes.o123;
                } else {
                    typ = mCard.comboTypes.o234;
                }
            } else if (c1 == 2 && c2 == 7 && c3 == 10) {
                typ = mCard.comboTypes.o2710;
            } else if (c1 == 12 && c2 == 17 && c3 == 20) {
                typ = mCard.comboTypes.o2710;
            } else if (mRoom.is1510 == true && c1 == 1 && c2 == 5 && c3 == 10) {
                typ = mCard.comboTypes.o2710;
            } else if (mRoom.is1510 == true && c1 == 11 && c2 == 15 && c3 == 20) {
                typ = mCard.comboTypes.o2710;
            } else {
                typ = mCard.comboTypes.jiao;
            }
        } else if (combo.length == 2) {
            typ = mCard.comboTypes.dui;
        }
        return typ;
    },
    //显示手牌里的胡息
    getHandsHuXi: function (hands) {
        var handhuxi = 0;
        for (var i = 0; i < hands.length; i++) {
            var typ = mCard.getComboType(hands[i], true);
            var huxi = mCard.getComboHuXi({cards: hands[i], typ: typ});
            // console.log(huxi + "===" + typ);
            handhuxi += huxi;
        }
        return handhuxi;
    },
    //显示的胡息  自己的话计算全部   不是自己不计算偎
    getHuXi: function (comboList, isSelf) {
        var total = 0;
        for (var i = 0; i < comboList.length; i++) {
            var comboInfo = comboList[i];

            var xi = 0;
            var typ = comboInfo.typ;
            //偎不计算胡息
            if (isSelf == null || isSelf == undefined || isSelf == true) {
                if (comboInfo.cards[0] > 10) {
                    xi = mCard.combo_hx_Big[typ - 1];
                } else {
                    xi = mCard.combo_hx_Small[typ - 1];
                }
                total += xi;
            } else {
                if (typ != mCard.comboTypes.wei) {
                    if (comboInfo.cards[0] > 10) {
                        xi = mCard.combo_hx_Big[typ - 1];
                    } else {
                        xi = mCard.combo_hx_Small[typ - 1];
                    }
                    total += xi;
                }
            }
        }
        return total;
    },
    //偎麻雀的胡息
    //碰   2 3
    //偎   3 4
    //将   0 2
    //团圆 碰(偎)+一句话  3 4
    //转弯 将+一句话     2 3
    getHuXiWeiMaQue: function (comboList) {
        var total = 0;
        var cardNumArray = [];
        for (var i = 0; i < comboList.length; i++) {
            var comboInfo = comboList[i];
            var xi = 0;
            var typ = comboInfo.typ;
            if (typ == mCard.comboTypes.peng) {
                if ([2, 7, 10, 12, 17, 20].indexOf(comboInfo.cards[0]) >= 0) {
                    total += 3;
                } else {
                    total += 2;
                }
            } else if (typ == mCard.comboTypes.wei || typ == mCard.comboTypes.chouwei) {
                if ([2, 7, 10, 12, 17, 20].indexOf(comboInfo.cards[0]) >= 0) {
                    total += 4;
                } else {
                    total += 3;
                }
            } else if (typ == mCard.comboTypes.dui) {
                if ([2, 7, 10, 12, 17, 20].indexOf(comboInfo.cards[0]) >= 0) {
                    total += 2;
                } else {
                    total += 0;
                }
            }
            total += xi;
            for (var j = 0; j < comboInfo.cards.length; j++) {
                if (cardNumArray[comboInfo.cards[j]]) {
                    cardNumArray[comboInfo.cards[j]]++;
                } else {
                    cardNumArray[comboInfo.cards[j]] = 1;
                }
            }
        }
        //团圆
        var hasWeiPeng = function (card) {
            for (var i = 0; i < comboList.length; i++) {
                var comboInfo = comboList[i];
                if ((comboInfo.typ == mCard.comboTypes.chouwei || comboInfo.typ == mCard.comboTypes.peng) &&
                    comboInfo.cards.indexOf(card)) {
                    return true;
                }
            }
            return false;
        };
        var hasYiJuHua = function (card) {
            for (var i = 0; i < comboList.length; i++) {
                var comboInfo = comboList[i];
                if (comboInfo.typ == mCard.comboTypes.o234 &&
                    comboInfo.cards.indexOf(card)) {
                    return true;
                }
            }
            return false;
        };
        for (var card in cardNumArray) {
            if (cardNumArray[card] == 4 && hasYiJuHua(card) && hasWeiPeng(card)) {
                total += ([2, 7, 10, 12, 17, 20].indexOf(parseInt(card)) >= 0) ? 4 : 3;
            }
        }
        return total;
    },
    //团圆 碰(偎)+一句话  3 4
    //转弯 将+一句话     2 1
    getComboHuXiWeiMaQue: function (comboInfo, comboList) {
        // console.log(comboInfo);
        // console.log(comboList);
        var cardNumArray = [];
        if (comboList) {
            for (var i = 0; i < comboList.length; i++) {
                var combo = comboList[i];
                for (var j = 0; j < combo.cards.length; j++) {
                    if (cardNumArray[combo.cards[j]]) {
                        cardNumArray[combo.cards[j]]++;
                    } else {
                        cardNumArray[combo.cards[j]] = 1;
                    }
                }
            }
        }

        var xi = 0;
        var typ = comboInfo.typ;
        if (typ == mCard.comboTypes.peng ||
            typ == mCard.comboTypes.wei || typ == mCard.comboTypes.chouwei || typ == mCard.comboTypes.kan) {
            if (typ == mCard.comboTypes.peng) {
                if ([2, 7, 10, 12, 17, 20].indexOf(comboInfo.cards[0]) >= 0) {
                    xi = 3;
                } else {
                    xi = 2;
                }
            } else {
                if ([2, 7, 10, 12, 17, 20].indexOf(comboInfo.cards[0]) >= 0) {
                    xi = 4;
                } else {
                    xi = 3;
                }
            }
            //团圆 碰(偎)+一句话  3 4
            for (var card in cardNumArray) {
                if (cardNumArray[card] == 4 && comboInfo.cards[0] == card) {
                    xi += ([2, 7, 10, 12, 17, 20].indexOf(parseInt(card)) >= 0) ? 4 : 3;
                    break;
                }
            }
        } else if (typ == mCard.comboTypes.dui) {
            if ([2, 7, 10, 12, 17, 20].indexOf(comboInfo.cards[0]) >= 0) {
                xi = 2;
            } else {
                xi = 0;
            }
            //转弯 将+一句话     2 3
            if (comboList) {
                for (var j = 0; j < comboList.length; j++) {
                    var combo = comboList[j];
                    if (combo.typ == mCard.comboTypes.o234 || combo.typ == mCard.comboTypes.o123 ||
                        combo.typ == mCard.comboTypes.o2710
                        || combo.typ == mCard.comboTypes.chi) {
                        if (combo.cards.indexOf(comboInfo.cards[0]) >= 0) {
                            if ([2, 7, 10, 12, 17, 20].indexOf(comboInfo.cards[0]) >= 0) {
                                xi += 1;
                            } else {
                                xi += 2;
                            }
                            break;
                        }
                    }
                }
            }
        }
        return xi;
    },
    getComboHuXi: function (comboInfo) {
        var xi = 0;
        var typ = comboInfo.typ;

        if (comboInfo.cards[0] > 10) {
            xi = mCard.combo_hx_Big[typ - 1];
        } else {
            xi = mCard.combo_hx_Small[typ - 1];
        }
        return xi;
    },

    getComboHuXiByType: function (typ, isBig) {
        var xi = 0;
        if (isBig) {
            xi = mCard.combo_hx_Big[typ - 1];
        } else {
            xi = mCard.combo_hx_Small[typ - 1];
        }
        return xi;
    },

    getCardList: function (cards) {
        cards = cards || mRoom['CardList'];
        // mRoom.wanfatype = mRoom.YAYA;

        var allCard = [];
        for (var i = 0; i < cards.length; i++) {
            allCard.push(cards[i]);
        }
        allCard.sort(function (a, b) {
            return a - b;
        });
        mCard.allCard = allCard;
        var retList = [];

        if (gameData.hz_sortId == undefined)
            gameData.hz_sortId = 2;
        if (gameData.hz_sortId == 2) {
            //排序全部采用长沙
            mCard.getCardListWork(retList, allCard, true);
            mCard.getCardListWork(retList, allCard, false);
            //排序
            retList.sort(function (a, b) {
                var isKan = function (kan) {
                    if (kan.length == 3 && kan[0] == kan[1] && kan[0] == kan[2]) return true;
                    return false;
                };
                var aquan = 0;
                var bquan = 0;
                if (isKan(a)) {
                    aquan = a[0];
                } else {
                    aquan = 100 + (a[0] + 9) % 10 * 10 + (a[0] + 9) / 10;
                }
                if (isKan(b)) {
                    bquan = b[0];
                } else {
                    bquan = 100 + (b[0] + 9) % 10 * 10 + (b[0] + 9) / 10;
                }
                return aquan - bquan;
            });
        } else if (gameData.hz_sortId == 1) {
            mCard.getCardListWorkLD1(retList, allCard);
        } else {
            retList = mCard.getCardSotrPerfect(retList, allCard);
        }
        network.sendPhz(5000, 'resetCardType/settingData.hz_sortId/' + gameData.hz_sortId);

        return retList;
    },
    getOtherCardList: function (cards) {
        var smallCard = [];
        var bigCard = [];
        if (!cards)
            return;
        for (var i = 0; i < cards.length; i++) {
            if (cards[i] > 10)
                bigCard.push(cards[i]);
            else if (cards[i] < 11)
                smallCard.push(cards[i]);
        }
        smallCard.sort(function (a, b) {
            return a - b;
        });
        bigCard.sort(function (a, b) {
            return a - b;
        });
        var retList = [];
        mCard.getCardListWork2(retList, smallCard, true);
        mCard.getCardListWork2(retList, bigCard, true);
        mCard.getCardListWork2(retList, smallCard, false);
        mCard.getCardListWork2(retList, bigCard, false, true);
        //排序
        retList.sort(function (a, b) {
            var isKan = function (kan) {
                if (kan.length == 3 && kan[0] == kan[1] && kan[0] == kan[2]) return true;
                return false;
            };
            var aquan = (isKan(a) ? 0 : 100) + a[0];
            var bquan = (isKan(b) ? 0 : 100) + b[0];
            return aquan - bquan;
        });
        return retList;
    },
    getCardListWork: function (retList, newCardList, isKan) {
        if (isKan) {
            mCard.searchSameCard(retList, newCardList, 4);
            mCard.searchSameCard(retList, newCardList, 3);
        } else {
            mCard.searchSameCard(retList, newCardList, 2);
            mCard.searchJiaoCard(retList, newCardList);
            mCard.search123Card(retList, newCardList, true);
            if (mRoom.wanfatype != mRoom.WEIMAQUE) {
                mCard.search2710Card(retList, newCardList, true);
                mCard.search2710Card(retList, newCardList, false);
            }
            if (mRoom.is1510 == true) {
                mCard.search1510Card(retList, newCardList, true);
                mCard.search1510Card(retList, newCardList, false);
            }
            mCard.search123Card(retList, newCardList);
            //再找   对能不能升级成  交
            if (mRoom.wanfatype != mRoom.WEIMAQUE) mCard.searchDuiToJiao(retList, newCardList);
            mCard.search123NeedCard(retList, newCardList);
            mCard.searchLianCard(retList, newCardList);//连着的牌
            for (var i = 0; i < newCardList.length; i++) {
                var combo = [];
                if (retList.length < 10) {
                    combo.push(newCardList[i]);
                    retList.push(combo);
                } else {
                    for (var j = retList.length - 1; j > 0; j--) {
                        if (retList[j].length < 3) {
                            retList[j].push(newCardList[i]);
                            break;
                        }
                    }
                }
            }
        }
        return retList;
    },
    getCardListWork2: function (retList, newCardList, isKan) {
        if (isKan) {
            mCard.searchSameCard(retList, newCardList, 4);
            mCard.searchSameCard(retList, newCardList, 3);
        } else {
            mCard.search123Card(retList, newCardList, true);
            mCard.searchJiaoCard(retList, newCardList);
            if (mRoom.wanfatype != mRoom.WEIMAQUE) {
                mCard.search2710Card(retList, newCardList, true);
                mCard.search2710Card(retList, newCardList, false);
            }
            if (mRoom.is1510 == true) {
                mCard.search1510Card(retList, newCardList, true);
                mCard.search1510Card(retList, newCardList, false);
            }
            mCard.searchSameCard(retList, newCardList, 2);
            mCard.search123Card(retList, newCardList);
            //再找   对能不能升级成  交
            if (mRoom.wanfatype != mRoom.WEIMAQUE) mCard.searchDuiToJiao(retList, newCardList);
            mCard.search123NeedCard(retList, newCardList);
            mCard.searchLianCard(retList, newCardList);//连着的牌
            for (var i = 0; i < newCardList.length; i++) {
                var combo = [];
                combo.push(newCardList[i]);
                retList.push(combo);
            }
        }
        return retList;
    },
    //从小到大排列，没有顺子
    getCardListWorkLD1: function (retList, allCard, noSort) {
        //先找出相同的
        var array4 = this.getSame(allCard, 4);
        var array3 = this.getSame(allCard, 3);
        var array2 = this.getSame(allCard, 2);
        this.removeArray(allCard, array4);
        this.removeArray(allCard, array3);
        this.removeArray(allCard, array2);
        //合并到数组
        retList.push.apply(retList, array4);
        retList.push.apply(retList, array3);
        retList.push.apply(retList, array2);
        //剩余
        this.cardListWorkPublic(retList, allCard, true);
    },
    //摆牌方式公用
    cardListWorkPublic: function (retList, allCard, isSort) {
        //找出3张连续
        var arrayStreak = [];
        for (var i = 0; i < 7; i++) {
            var arrayTemp = this.get3CertainNumber(allCard, i + 1, i + 2, i + 3);
            this.removeArray(allCard, arrayTemp);
            arrayStreak = arrayStreak.concat(arrayTemp);
            arrayTemp = this.get3CertainNumber(allCard, i + 11, i + 12, i + 13);
            this.removeArray(allCard, arrayTemp);
            arrayStreak = arrayStreak.concat(arrayTemp);
        }
        //再找出2张连续
        for (var i = 0; i < 9; i++) {
            var arrayTemp = this.get2CertainNumber(allCard, i + 1, i + 2);
            this.removeArray(allCard, arrayTemp);
            arrayStreak = arrayStreak.concat(arrayTemp);
            arrayTemp = this.get2CertainNumber(allCard, i + 11, i + 12);
            this.removeArray(allCard, arrayTemp);
            arrayStreak = arrayStreak.concat(arrayTemp);
        }
        //合并到数组，除了纯单牌
        retList.push.apply(retList, arrayStreak);
        for (var i = 0; i < allCard.length; i++) {
            var count = 0;
            for (var j = 0; j < mCard.allCard.length; j++) {
                if (allCard[i] == mCard.allCard[j]) {
                    count++;
                    if (count == 2) {
                        retList.push([allCard[i]]);
                        allCard.splice(i, 1);
                        break;
                    }
                }
            }
        }
        //把数组按首元素大小排序
        if (isSort) {
            retList.sort(function (a, b) {
                return a[0] % 10 - b[0] % 10;
            });
            if (retList[0]) {
                if (retList[0][0] == 20) {
                    retList.push(retList[0]);
                    retList.splice(0, 1);
                }
                if (retList[0][0] == 10) {
                    retList.push(retList[0]);
                    retList.splice(0, 1);
                }
            }
        }
        //不是纯单的牌一起放最后
        var tempArray = [];
        while (allCard.length > 0) {
            tempArray.push(allCard[0]);
            allCard.splice(0, 1);
            if (tempArray.length == 3) {
                retList.push(tempArray);
                tempArray = [];
            }
        }
        if (tempArray.length > 0) retList.push(tempArray);
        //当大于11列时，牌往前放
        if (retList.length > 11) {
            var cardArray = [];
            for (var i = retList.length - 1; i >= 11; i--) {
                cardArray = cardArray.concat(retList[i]);
            }
            for (var i = 10; i >= 0; i--) {
                if (retList[i].length < 3) {
                    retList[i] = retList[i].concat(cardArray.splice(0, 3 - retList[i].length));
                }
                if (cardArray.length == 0) break;
            }
            retList.splice(11, retList.length - 1);
        }
    },
    //获取指定3张牌
    get3CertainNumber: function (cards, num1, num2, num3) {
        var array1 = [], array2 = [], array3 = [];
        for (var i = 0; i < cards.length; i++) {
            if (cards[i] == num1) array1.push(cards[i]);
            else if (cards[i] == num2) array2.push(cards[i]);
            else if (cards[i] == num3) array3.push(cards[i]);
        }
        var min = Math.min(array1.length, array2.length, array3.length);
        var array = [];
        for (var i = 0; i < min; i++) {
            array.push([array1[i], array2[i], array3[i]]);
        }
        return array;
    },
    //获取指定2张牌
    get2CertainNumber: function (cards, num1, num2) {
        var array1 = [], array2 = [];
        for (var i = 0; i < cards.length; i++) {
            if (cards[i] == num1) array1.push(cards[i]);
            else if (cards[i] == num2) array2.push(cards[i]);
        }
        var min = Math.min(array1.length, array2.length);
        var array = [];
        for (var i = 0; i < min; i++) {
            array.push([array1[i], array2[i]]);
        }
        return array;
    },
    //从排好序的牌中得到指定数量相同的牌
    getSame: function (allCard, sameNum) {
        var presentValue = 0;
        var numCount = 0;
        var array = [];
        for (var i = 0; i < allCard.length; i++) {
            if (presentValue == allCard[i]) {
                numCount++;
                if (i == allCard.length - 1 && numCount == sameNum) {
                    array.push(allCard.slice(i - numCount + 1, i + 1));
                }
            } else {
                if (numCount == sameNum) {
                    array.push(allCard.slice(i - numCount, i));
                }
                presentValue = allCard[i];
                numCount = 1;
            }
        }
        return array;
    },
    //数组移除相同的元素
    removeArray: function (array, deleteArray) {
        for (var i = 0; i < deleteArray.length; i++) {
            for (var j = 0; j < deleteArray[i].length; j++) {
                for (var k = 0; k < array.length; k++) {
                    if (deleteArray[i][j] == array[k]) {
                        array.splice(k, 1);
                        break;
                    }
                }
            }
        }
    },

    getCardSotrPerfect: function (retList, newCardList) {
        // showToast("最新版本理牌");
        // console.log('号称完美的理牌');
        //第一优先级  提(四张)  叠加在一起
        mCard.searchSameCard(retList, newCardList, 4);
        // console.log('第一条执行完:' + JSON.stringify(retList));
        //第二优先级  坎(三张)  叠加在一起
        mCard.searchSameCard(retList, newCardList, 3);
        // console.log('第二条执行完:' + JSON.stringify(retList));
        //第三优先级  两张叠加在一起
        mCard.searchSameCard(retList, newCardList, 2);
        // console.log('第三条执行完:' + JSON.stringify(retList));
        //第四优先级 单张中可以组成一句话的在一起 (优先 1、2、3 --- 2、7、10)
        mCard.search123Card(retList, newCardList, true);   //严格查找 1、2、3的顺子
        mCard.search2710Card(retList, newCardList, true);  //严格查找 2、7、10
        mCard.search2710Card(retList, newCardList, false);
        mCard.search123Card(retList, newCardList); //查找一句话(顺子)
        // console.log('第四条执行完:' + JSON.stringify(retList));
        //第五优先级 姐妹牌 (小1小2 小3小4)
        mCard.searchJieMeiPai(retList, newCardList);
        // console.log('第五执行完:' + JSON.stringify(retList));
        //第六优先级 大小牌 (数字一样  大小不同)
        mCard.searchDaXiaoPai(retList, newCardList, 3);
        mCard.searchDaXiaoPai(retList, newCardList, 2);
        // console.log('第六执行完:' + JSON.stringify(retList));
        //第七优先级 隔辈牌
        mCard.searchGeBeiPai(retList, newCardList, true);
        mCard.searchGeBeiPai2710(retList, newCardList);
        mCard.searchGeBeiPai(retList, newCardList);
        // console.log('第七执行完:' + JSON.stringify(retList));
        //第八条 和 第九条 都在这个方法里处理
        var errorSend = mCard.searchShengYuPai(retList, newCardList);
        // console.log('第八执行完:' + JSON.stringify(retList));
        //最终列排序11
        retList = mCard.searchColumnSort(retList);
        if (errorSend) {
            network.sendPhz(5000, 'lipaiError/123456789/retList:/' + JSON.stringify(retList));
        }

        return retList;
    },

    /**
     * 最终列的排序
     * 除去 提和坎的列 所有列按照最下方数字：壹、一、贰、二 的顺序进行列的排序
     * @param list
     */
    searchColumnSort: function (list) {
        var cardsThree = [];
        var cards = [];
        for (var i = 0; i < list.length; i++) {
            list[i].sort(function (a, b) {
                var tempA = a > 10 ? a - 10 : a;
                var tempB = b > 10 ? b - 10 : b;
                if (tempA == tempB && a > b) {
                    return b - a;
                }
                return tempA - tempB;
            });
            if (list[i].length >= 3 && (list[i][0] == list[i][1] && list[i][0] == list[i][2])) {
                cardsThree.push(list[i]);
            } else {
                cards.push(list[i]);
            }
        }

        for (var i = 0; i < cards.length; i++) {
            for (var j = i + 1; j < cards.length; j++) {
                var cardI = cards[i][0];
                var cardJ = cards[j][0];
                cardI = cardI > 10 ? cardI - 10 : cardI;
                cardJ = cardJ > 10 ? cardJ - 10 : cardJ;
                // 1          1         1             11
                if (cardI == cardJ && cards[i][0] < cards[j][0]) {
                    var temp = cards[i];
                    cards[i] = cards[j];
                    cards[j] = temp;
                } else if (cardI > cardJ) {
                    var temp = cards[i];
                    cards[i] = cards[j];
                    cards[j] = temp;
                }
            }
        }


        // cards.sort(function (cardsA, cardsB) {
        //     var a = cardsA[0];
        //     var b = cardsB[0];
        //     var tempA = a > 10 ? a - 10 : a;
        //     var tempB = b > 10 ? b - 10 : b;
        //     if(tempA == tempB && a > b){
        //         return b - a;
        //     }
        //     return tempA - tempB;
        // });
        return cardsThree.concat(cards);
    },


    //第八条 和 第九条 都在这个方法里处理
    searchShengYuPai: function (list, cards) {
        //如果进入到第九条 或者 第八条的cards存在剩余的散排给服务器发送内容
        var errorSend = false;
        //如果现在理出的牌的长度 和上下的散排的长度小于
        //最大上线的长度则直接把散牌单独成为一列
        //第八条 处理剩余牌 所有牌单独成为一列
        var deleteList = [];
        if ((list.length + cards.length) <= 11) {
            for (var i = 0; i < cards.length; i++) {
                var combo = [];
                combo.push(cards[i]);
                deleteList.push(i);
                list.push(combo);
                errorSend = true;
            }
        } else {
            //第九条 如果列数大于指定列数 则散排自动叠加
            //首先组合成壹、一、贰、二 的形式
            cards.sort(function (a, b) {
                var tempA = a > 10 ? a - 10 : a;
                var tempB = b > 10 ? b - 10 : b;
                if (tempA == tempB && a > b) {
                    return b - a;
                }
                return tempA - tempB;
            });
            for (var i = 0; i < cards.length;) {
                var combo = [];
                var two = i + 1;
                var three = i + 2;

                combo.push(cards[i]);
                deleteList.push(i);
                i++;
                //用最笨定的办法 把剩余的散牌 每三个放到一列
                if (cards[two] != undefined) {
                    combo.push(cards[two]);
                    deleteList.push(two);
                    i++;
                }

                if (cards[three] != undefined) {
                    combo.push(cards[three]);
                    deleteList.push(three);
                    i++;
                }
                list.push(combo);
            }
            errorSend = true;
        }
        removeInArray(cards, deleteList);
        return errorSend;
    },
    //第六优先级 大小牌 (数字一样  大小不同)
    searchDaXiaoPai: function (list, cards, num) {
        if (num == undefined) {
            console.log('需要传入count');
            return;
        }
        var sameList = {};
        for (var i = 0; i < cards.length; i++) {
            var card = cards[i];
            var cardNumber = card;
            if (cardNumber > 10) cardNumber -= 10;

            if (sameList[cardNumber] == null) {
                sameList[cardNumber] = {};
                sameList[cardNumber].cards = [];
                sameList[cardNumber].pos = [];
                sameList[cardNumber].count = 1;
            } else {
                sameList[cardNumber].count += 1;
            }
            sameList[cardNumber].cards.push(card);
            sameList[cardNumber].pos.push(i);
        }

        var deleteList = [];
        for (var key in sameList) {
            var count = sameList[key].count;
            if (count == num) {
                var combo = sameList[key].cards;
                for (var s = 0; s < combo.length; s++) {
                    if (combo.length == 3 && combo[0] != combo[1]) {
                        var tmp = combo[0];
                        combo[0] = combo[2];
                        combo[2] = tmp;
                    }
                }
                list.push(combo);
                deleteList = deleteList.concat(sameList[key].pos);
            } else if (count == 1) {
                var card = sameList[key].cards[0];
                for (var i = 0; i < list.length; i++) {
                    if (list[i].length == 2) {
                        var listValue = list[i][0];
                        var listValue2 = list[i][1];
                        if (listValue == listValue2) {
                            listValue = listValue > 10 ? listValue - 10 : listValue;
                            card = card > 10 ? card - 10 : card;
                            if (card == listValue) {
                                list[i].push(sameList[key].cards[0]);
                                deleteList = deleteList.concat(sameList[key].pos);
                            }
                        }
                    }
                }
            }

        }
        removeInArray(cards, deleteList);
    },
    //第五优先级 姐妹牌 (小1小2 小3小4)
    searchJieMeiPai: function (list, cards) {
        var deleteList = [];
        for (var i = 0; i < cards.length - 1; i++) {
            var card1 = cards[i];
            for (var j = i + 1; j < cards.length; j++) {
                var card2 = cards[j];
                if (card2 == card1 + 1 && card2 != 11) {
                    var isInUse = false;
                    for (var x = 0; x < deleteList.length; x++) {
                        var y = deleteList[x];
                        if (i == y || j == y) {
                            isInUse = true;
                            break;
                        }
                    }

                    if (isInUse == false) {
                        var combo = [card1, card2];
                        list.push(combo);
                        deleteList.push(i);
                        deleteList.push(j);
                    }
                } else if (card2 > card1 + 1) {
                    break;
                }
            }
        }
        removeInArray(cards, deleteList);
    },
    //第七优先级 隔辈牌
    searchGeBeiPai2710: function (list, cards) {
        var deleteList = [];
        //循环所有牌数据
        for (var i = 0; i < cards.length - 1; i++) {
            var card1 = cards[i];
            if (card1 != 2 && card1 != 7 && card1 != 12 && card1 != 17) {
                continue;
            }
            for (var j = i + 1; j < cards.length; j++) {
                var card2 = cards[j];
                if (card2 == 7 || card2 == 10 || card2 == 17 || card2 == 20) {
                    var isInUse = false;
                    for (var x = 0; x < deleteList.length; x++) {
                        var y = deleteList[x];
                        if (i == y || j == y) {
                            isInUse = true;
                            break;
                        }
                    }

                    if (isInUse == false) {
                        var combo = [card1, card2];
                        list.push(combo);
                        deleteList.push(i);
                        deleteList.push(j);
                    }
                }
            }
        }
        removeInArray(cards, deleteList);
    },
    //第七优先级 隔辈牌
    searchGeBeiPai: function (list, cards, is13) {
        var deleteList = [];
        //循环所有牌数据
        for (var i = 0; i < cards.length - 1; i++) {
            var card1 = cards[i];
            //如果是强制查找 1 3 这中隔辈牌
            if (is13 == true && card1 != 1 && card1 != 11) {
                continue;
            }

            for (var j = i + 1; j < cards.length; j++) {
                var card2 = cards[j];
                if (card2 == card1 + 2 && card2 != 11 && card2 != 12) {
                    var isInUse = false;
                    for (var x = 0; x < deleteList.length; x++) {
                        var y = deleteList[x];
                        if (i == y || j == y) {
                            isInUse = true;
                            break;
                        }
                    }

                    if (isInUse == false) {
                        var combo = [card1, card2];
                        list.push(combo);
                        deleteList.push(i);
                        deleteList.push(j);
                    }
                } else if (card2 > card1 + 2) {
                    break;
                }
            }
        }
        removeInArray(cards, deleteList);
    },

    getCardCount: function (cards, card) {
        var count = 0;
        for (var i = 0; i < cards.length; i++) {
            if (cards[i] == card) {
                count++;
            }
        }
        return count;
    },

    searchSameCard: function (list, cards, num) {
        var sameList = {};
        for (var i = 0; i < cards.length; i++) {
            var card = cards[i];
            if (sameList[card] == null) {
                sameList[card] = {};
                sameList[card].count = 1;
                sameList[card].pos = i;
            } else {
                sameList[card].count += 1;
            }
        }

        var deleteList = [];
        for (var key in sameList) {
            var count = sameList[key].count;
            if (count == num) {
                var v = parseInt(key);
                var combo = [];
                for (var i = 0; i < count; i++) {
                    combo.push(v);
                    deleteList.push(sameList[key].pos + i);
                }
                list.push(combo);
            }
        }
        removeInArray(cards, deleteList);
    },

    search2710Card: function (list, cards, isBig) {
        var list1 = [];
        var list2 = [];
        var list3 = [];

        for (var i = 0; i < cards.length; i++) {
            var cardNumber = cards[i];
            if (isBig == true) {
                cardNumber = cardNumber - 10;
            }

            if (cardNumber == 2) {
                list1.push(i);
            } else if (cardNumber == 7) {
                list2.push(i);
            } else if (cardNumber == 10) {
                list3.push(i);
            }
        }

        var count = Math.min(list1.length, list2.length, list3.length);
        var deleteList = [];
        for (var i = 0; i < count; i++) {
            var combo = [2, 7, 10];
            if (isBig) {
                combo = [12, 17, 20];
            }
            list.push(combo);

            deleteList.push(list1[i]);
            deleteList.push(list2[i]);
            deleteList.push(list3[i]);
        }
        removeInArray(cards, deleteList);
    },

    search1510Card: function (list, cards, isBig) {
        var list1 = [];
        var list2 = [];
        var list3 = [];

        for (var i = 0; i < cards.length; i++) {
            var cardNumber = cards[i];
            if (isBig == true) {
                cardNumber = cardNumber - 10;
            }

            if (cardNumber == 1) {
                list1.push(i);
            } else if (cardNumber == 5) {
                list2.push(i);
            } else if (cardNumber == 10) {
                list3.push(i);
            }
        }

        var count = Math.min(list1.length, list2.length, list3.length);
        var deleteList = [];
        for (var i = 0; i < count; i++) {
            var combo = [1, 5, 10];
            if (isBig) {
                combo = [11, 15, 20];
            }
            list.push(combo);

            deleteList.push(list1[i]);
            deleteList.push(list2[i]);
            deleteList.push(list3[i]);
        }
        removeInArray(cards, deleteList);
    },
    searchDuiToJiao: function (retList, list) {
        var deleteList = [];
        for (var i = 0; i < retList.length; i++) {
            if (retList[i].length == 2 && retList[i][0] == retList[i][1]) {
                for (var j = 0; j < list.length; j++) {
                    if (retList[i][0] != 21 && ((list[j] + 10 == retList[i][0]) || (list[j] - 10 == retList[i][0]))) {
                        deleteList.push(j);
                        retList[i].push(list[j]);
                        break;
                    }
                }
            }
        }
        removeInArray(list, deleteList);
    },
    search123Card: function (list, cards, iso123) {
        var deleteList = [];
        for (var i = 0; i < cards.length - 2; i++) {
            var card1 = cards[i];

            if (iso123 == true && card1 != 1 && card1 != 11) {
                continue;
            }

            for (var j = i + 1; j < cards.length - 1; j++) {
                var card2 = cards[j];
                if (card2 == card1 + 1) {
                    for (var k = j + 1; k < cards.length; k++) {
                        var card3 = cards[k];
                        if (card3 == card2 + 1 && card3 != 11 && card3 != 12) {
                            var isInUse = false;
                            for (var x = 0; x < deleteList.length; x++) {
                                var y = deleteList[x];
                                if (i == y || j == y || k == y) {
                                    isInUse = true;
                                    break;
                                }
                            }

                            if (isInUse == false) {
                                var combo = [card1, card2, card3];
                                list.push(combo);
                                deleteList.push(i);
                                deleteList.push(j);
                                deleteList.push(k);
                            }
                        } else if (card3 > card2 + 1) {
                            break;
                        }
                    }
                } else if (card2 > card1 + 1) {
                    break;
                }
            }
        }
        removeInArray(cards, deleteList);
    },

    searchJiaoCard: function (list, cards) {
        var sameList = {};
        for (var i = 0; i < cards.length; i++) {
            var card = cards[i];
            var cardNumber = card;
            if (cardNumber > 10) cardNumber -= 10;

            if (sameList[cardNumber] == null) {
                sameList[cardNumber] = {};
                sameList[cardNumber].cards = [];
                sameList[cardNumber].pos = [];
                sameList[cardNumber].count = 1;
            } else {
                sameList[cardNumber].count += 1;
            }
            sameList[cardNumber].cards.push(card);
            sameList[cardNumber].pos.push(i);
        }

        var deleteList = [];
        for (var key in sameList) {
            var count = sameList[key].count;
            if (count == 3) {
                var combo = sameList[key].cards;
                for (var s = 0; s < combo.length; s++) {
                    if (combo.length == 3 && combo[0] != combo[1]) {
                        var tmp = combo[0];
                        combo[0] = combo[2];
                        combo[2] = tmp;
                    }
                }
                list.push(combo);
                deleteList = deleteList.concat(sameList[key].pos);
            }
        }
        removeInArray(cards, deleteList);
    },

    is2710: function (card) {
        var no = card;
        if (no > 10) no -= 10;
        if (no == 2 || no == 7 || no == 10) {
            return true;
        }
        return false;
    },

    is1510: function (card) {
        var no = card;
        if (no > 10) no -= 10;
        if (no == 1 || no == 5 || no == 10) {
            return true;
        }
        return false;
    },

    search123NeedCard: function (list, cards) {
        var getList = [];
        var deleteList = [];

        for (var i = 0; i < cards.length - 1; i++) {
            var card1 = cards[i];
            var card2 = cards[i + 1];

            if (mCard.is2710(card1) && mCard.is2710(card2)) {
                var combo = [card1, card2];
                getList.push(i);
                list.push(combo);

                deleteList.push(i);
                deleteList.push(i + 1);

                i += 1;
            }
            else {
                if ((card2 == card1 + 1 || card2 == card1 + 2) && (card2 != 11 && card1 != 10)) {
                    var combo = [card1, card2];
                    getList.push(i);
                    list.push(combo);

                    deleteList.push(i);
                    deleteList.push(i + 1);

                    i += 1;
                }
            }
        }
        removeInArray(cards, deleteList);
    },
    //连着的牌
    searchLianCard: function (list, cards) {
        var deleteList = [];
        for (var i = cards.length - 1; i > 0; i--) {
            for (var j = i - 1; j >= 0; j--) {
                if (cards[i] && cards[j]) {
                    var card2 = cards[i];
                    var card1 = cards[j];
                    if (((card2 == card1 + 1 || card2 == card1 + 2) && card2 != 11 && card1 != 10) ||
                        (card2 == card1 + 10 && card2 != 21)) {
                        var combo = [card1, card2];
                        list.push(combo);
                        deleteList = [];
                        deleteList.push(j);
                        deleteList.push(i);
                        removeInArray(cards, deleteList);
                    }
                }
            }
        }
    },

    getHuCardCountInfo: function (huInfo) {
        var info = {};
        info.big = 0;
        info.small = 0;
        info.red = 0;

        if (huInfo != null) {
            mCard.getCardCountInComboList(huInfo.openList, info);
            mCard.getCardCountInComboList(huInfo.kanList, info);
            mCard.getCardCountInComboList(huInfo.huList, info);
        }
        return info;
    },

    getCardCountInComboList: function (comboList, info) {
        for (var i = 0; i < comboList.length; i++) {
            var comboInfo = comboList[i];
            var cards = comboInfo.cards;
            for (var j = 0; j < cards.length; j++) {
                var card = cards[j];
                if (card > 10) {
                    info.big += 1;
                } else {
                    info.small += 1;
                }
                if (mCard.is2710(card) == true) {
                    info.red += 1;
                }
            }
        }
    }
};


/*mEffect.js*/


var mEffect = {
    liangPai: function (showCard, userPos) {
        // var img = showCard.getChildByName("img");
        var img = showCard;
        img.stopAllActions();
        showCard.stopAllActions();
        var begin_pos;
        var show_pos;
        var end_pos;
        begin_pos = cc.p(1280 / 2, 520);
        show_pos = cc.p(1280 / 2, 350);
        if (gameData.playerNum == 3) {
            if (userPos == 0) {
                end_pos = cc.p(1280 / 2, 100);
            } else if (userPos == 1) {
                end_pos = cc.p(1280 - 100, 350);
            } else {
                end_pos = cc.p(100, 350);
            }
        } else if (gameData.playerNum == 2) {
            if (userPos == 0) {
                end_pos = cc.p(1280 / 2, 100);
            } else if (userPos == 1) {
                end_pos = cc.p(1280 - 100, 350);
            }
        } else {
            if (userPos == 0) {
                end_pos = cc.p(640, 100);
            } else if (userPos == 1) {
                end_pos = cc.p(1180, 350);
            } else if (userPos == 2) {
                end_pos = cc.p(640, 350);
            } else {
                end_pos = cc.p(100, 350);
            }
        }
        showCard.setPosition(begin_pos);
        img.setScale(0.5);
        if (mRoom.isReplay) {
            if (!end_pos)
                return;
            showCard.runAction(cc.sequence(
                cc.moveTo(0.2, show_pos),
                cc.delayTime(0.5),
                cc.moveTo(0.2, end_pos),
                cc.callFunc(function () {
                    showCard.setVisible(false);
                })
            ));
        } else {
            showCard.runAction(cc.sequence(
                cc.moveTo(0.2, show_pos),
                cc.delayTime(0.5),
                cc.moveTo(0.2, end_pos),
                cc.callFunc(function () {
                    showCard.setVisible(false);
                })
            ));
        }
        img.runAction(cc.sequence(
            cc.scaleTo(0.2, 1),
            cc.delayTime(0.2 + 2),
            cc.scaleTo(0.2, 0.5)
        ));
    },
    chuPai: function (showCard, userPos) {
        // var img = showCard.getChildByName("img");
        var img = showCard;
        img.stopAllActions();
        showCard.stopAllActions();
        var begin_pos;
        var end_pos;
        var rotated = null;
        if (gameData.playerNum == 3) {
            if (userPos == 0) {
                begin_pos = cc.p(1280 / 2 - 8, 100);
                end_pos = cc.p(1280 / 2 - 8, 570);
            } else if (userPos == 1) {
                begin_pos = cc.p(1280 - 160, 350);
                end_pos = cc.p(1280 - 260, 500);
            } else {
                begin_pos = cc.p(160, 350);
                end_pos = cc.p(260, 500);
            }
        } else if (gameData.playerNum == 2) {
            if (userPos == 0) {
                begin_pos = cc.p(1280 / 2 - 8, 100);
                end_pos = cc.p(1280 / 2 - 8, 570);
            } else if (userPos == 1) {
                begin_pos = cc.p(1280 - 160, 350);
                end_pos = cc.p(1280 - 260, 500);
            }
        } else {
            if (mRoom.wanfatype == mRoom.HENGYANG || mRoom.wanfatype == mRoom.MAOHUZI) {
                if (userPos == 0) {
                    begin_pos = cc.p(1280 / 2, 100);
                    end_pos = cc.p(1280 / 2, 450);
                    rotated = 90;
                } else if (userPos == 1) {
                    begin_pos = cc.p(1280 - 160, 150);
                    end_pos = cc.p(1280 - 260, 300);
                    rotated = 0;
                } else if (userPos == 2) {
                    begin_pos = cc.p(1280 - 160, 600);
                    end_pos = cc.p(1280 - 260, 500);
                    rotated = 90;
                } else {
                    begin_pos = cc.p(100, 600);
                    end_pos = cc.p(200, 500);
                    rotated = 0;
                }
            } else {
                if (userPos == 0) {
                    begin_pos = cc.p(640, 100);
                    end_pos = cc.p(500, 320);
                    rotated = 90;
                } else if (userPos == 1) {
                    begin_pos = cc.p(1180, 320);
                    end_pos = cc.p(800, 350);
                    rotated = 0;
                } else if (userPos == 2) {
                    begin_pos = cc.p(640, 500);
                    end_pos = cc.p(500, 510);
                    rotated = 90;
                } else {
                    begin_pos = cc.p(100, 350);
                    end_pos = cc.p(200, 350);
                    rotated = 0;
                }
            }

        }
        if (mRoom.wanfatype == mRoom.HENGYANG || mRoom.wanfatype == mRoom.MAOHUZI) {
        } else {
            if (rotated != null) {
                showCard.setRotation(rotated);
            }
        }
        showCard.setPosition(begin_pos);
        img.setScale(0.5);
        showCard.runAction(cc.moveTo(0.2, end_pos));
        img.runAction(cc.scaleTo(0.2, 1));
    },
    moPai: function (showCard, userPos) {
        var img = showCard;
        img.stopAllActions();
        showCard.stopAllActions();
        var begin_pos = cc.p(1280 / 2, 580);
        var end_pos;
        var rotated = null;
        var userNum = 3;
        if (mRoom.shouxing && mRoom.sxUserId > 0) {
            userNum = 4;
        }
        if (gameData.playerNum == userNum) {
            if (userPos == 0) {
                end_pos = cc.p(1280 / 2, 480);
            } else if (userPos == 1) {
                end_pos = cc.p(1280 - 260, 500);
            } else {
                end_pos = cc.p(260, 500);
            }
        } else if (gameData.playerNum == 2) {
            if (userPos == 0) {
                end_pos = cc.p(1280 / 2, 480);
            } else if (userPos == 1) {
                end_pos = cc.p(1280 - 260, 500);
            }
        } else {
            if (mRoom.wanfatype == mRoom.HENGYANG || mRoom.wanfatype == mRoom.QIYANG) {
                if (userPos == 0) {
                    end_pos = cc.p(1280 / 2, 450);
                } else if (userPos == 1) {
                    end_pos = cc.p(1280 - 260, 300);
                } else if (userPos == 2) {
                    end_pos = cc.p(1280 - 260, 500);
                } else {
                    end_pos = cc.p(200, 500);
                }
            } else {
                if (userPos == 0) {
                    begin_pos = cc.p(640, 100);
                    end_pos = cc.p(500, 320);
                    rotated = 90;
                } else if (userPos == 1) {
                    begin_pos = cc.p(1180, 320);
                    end_pos = cc.p(800, 300);
                    rotated = 0;
                } else if (userPos == 2) {
                    begin_pos = cc.p(640, 500);
                    end_pos = cc.p(500, 510);
                    rotated = 90;
                } else {
                    begin_pos = cc.p(100, 350);
                    end_pos = cc.p(200, 350);
                    rotated = 0;
                }
            }
        }
        if (mRoom.wanfatype == mRoom.HENGYANG || mRoom.wanfatype == mRoom.QIYANG
            || mRoom.wanfatype == mRoom.MAOHUZI) {
        } else {
            if (rotated != null) {
                showCard.setRotation(rotated);
            }
        }
        showCard.setPosition(begin_pos);
        img.setScale(0.5);
        if (end_pos) {
            showCard.runAction(
                cc.sequence(
                    cc.moveTo(0.15, end_pos),
                    cc.callFunc(function () {
                        // showCard.setVisible(false);
                    })
                )
            );
        }
        img.runAction(cc.scaleTo(0.15, 1));
    },

    //手牌   吃的哪几张牌
    chiPai: function (openCards, showPai) {
        // if(mRoom.isReplay){
        //     return;
        // }
        if (!cc.sys.isObjectValid(openCards) || !_.isFunction(openCards.getColIndex)) {
            return 'invalid';
        }
        var colIndex = openCards.getColIndex();
        var children = openCards.getChildren();
        for (var i = 0; i < children.length; i++) {
            var child = children[i];
            if (child.getTag() == colIndex) {
                child.setPositionX(child.getPositionX() + ((openCards.getPositionX() >= 1280 / 2) ? -250 : 250));
                var action = cc.sequence(
                    //cc.delayTime(0.6)
                    cc.moveBy(0.35, (openCards.getPositionX() >= 1280 / 2) ? 250 : -250, 0)
                    // ,cc.scaleTo(0.1, 1.5)
                    // ,cc.scaleTo(0.1, 1)
                );
                // child.stopAllActions();
                child.runAction(action);
            }
        }
    },
    //手牌   吃的哪几张牌
    chiPai2: function (openCards, userPos, ishide) {
        // if(mRoom.isReplay){
        //     return;
        // }
        var colIndex = openCards.getColIndex();
        var children = openCards.getChildren();
        var posY = 0;
        var child0 = null, child1 = null, child2 = null, child3 = null;
        for (var i = 0; i < children.length; i++) {
            var child = children[i];
            if (child.getTag() == colIndex) {
                if (userPos == 0) {
                    if (child.getName() == 'card0') {
                        posY = child.getPositionY();
                        console.log('child====scale=====' + child.getScale());
                    }
                    if (!ishide)
                        child.setData(child.data, null, 1);
                    child.setPositionX(child.getPositionX() + 605 - colIndex * 50);
                    if (ishide) {
                        child.setPositionY(child.getPositionY() + 270);
                    } else {
                        child.setPositionY(posY + 270);
                    }
                    var action = cc.sequence(
                        cc.delayTime(.35)
                        , cc.moveBy(.10, -605 + colIndex * 50, -270)
                        // ,cc.callFunc(function(){
                        //     child.setData(child.data, null, 2);
                        //     child.setScale(1.1);
                        //     if(ishide){
                        //         child.setOpacity(160);
                        //     }
                        // })
                        // ,cc.scaleTo(0.1, 1.5)
                        // ,cc.scaleTo(0.1, 1)
                    );
                    // child.stopAllActions();
                    child.runAction(action);
                    if (ishide)
                        continue;
                    if (child.getName() == 'card0') {
                        child0 = child;
                        child0.runAction(cc.sequence(
                            cc.delayTime(.35),
                            cc.scaleTo(.10, .5)
                            , cc.callFunc(function () {
                                for (var i = 0; i < children.length; i++) {
                                    var child_0 = children[i];
                                    if (child_0.getTag() == colIndex) {
                                        child_0.setData(child_0.data, null, 2);
                                        if (ishide)
                                            child_0.setOpacity(160);
                                        child_0.setScale(1.1);
                                    }
                                }
                            })
                        ));
                    }
                    if (child.getName() == 'card1') {
                        child1 = child;
                        child1.runAction(cc.moveBy(.15, 0, 45));
                        child1.runAction(cc.sequence(
                            cc.delayTime(.35),
                            cc.scaleTo(.10, .5),
                            cc.callFunc(function () {
                                child1.setScale(1.1);
                            })
                        ));
                    }
                    if (child.getName() == 'card2') {
                        child2 = child;
                        child2.runAction(cc.moveBy(.15, 0, 90)),
                            child2.runAction(cc.sequence(
                                cc.delayTime(.35),
                                cc.scaleTo(.10, .5),
                                cc.callFunc(function () {
                                    child2.setScale(1.1);
                                })
                            ));
                    }
                    if (child.getName() == 'card3') {
                        child3 = child;
                        child3.runAction(cc.moveBy(.15, 0, 135)),
                            child3.runAction(cc.sequence(
                                cc.delayTime(.35),
                                cc.scaleTo(.10, .5),
                                cc.callFunc(function () {
                                    child3.setScale(1.1);
                                })
                            ));
                    }
                } else {
                    if (child.getName() == 'card0') {
                        posY = child.getPositionY();
                        console.log('child====getPositionY=====' + child.getPositionY());
                    }
                    if (ishide) {
                        //child.setData(child.data, null, 1, true);
                    } else {
                        child.setData(child.data, null, 1);
                    }
                    child.setPositionX(child.getPositionX() + ((openCards.getPositionX() >= 1280 / 2) ? -250 : 250));
                    if (!ishide) {
                        child.setPositionY(posY);
                    }

                    var action = cc.sequence(
                        cc.delayTime(.35)
                        , cc.moveBy(.10, (openCards.getPositionX() >= 1280 / 2) ? 250 : -250, 0)
                        // ,cc.scaleTo(0.1, 1.5)
                        // ,cc.scaleTo(0.1, 1)
                    );
                    // child.stopAllActions();
                    child.runAction(action);
                    // child.runAction(cc.sequence(
                    //     cc.delayTime(.35),
                    //     cc.fadeTo(.10, 255)
                    // ));
                    if (ishide)
                        continue;
                    if (child.getName() == 'card0') {
                        child0 = child;
                        child0.runAction(cc.moveBy(.15, 0, 0));
                        child0.runAction(cc.sequence(
                            cc.delayTime(.35),
                            cc.scaleTo(.10, .5)
                            , cc.callFunc(function () {
                                for (var i = 0; i < children.length; i++) {
                                    var child_0 = children[i];
                                    if (child_0.getTag() == colIndex) {
                                        if (ishide) {
                                            child_0.setData(child_0.data, null, 2, true);
                                        } else {
                                            child_0.setData(child_0.data, null, 2);
                                        }
                                        child_0.setScale(1.1);
                                    }
                                }
                            })
                        ));
                    }
                    if (child.getName() == 'card1') {
                        child1 = child;
                        child1.runAction(cc.moveBy(.15, 0, 45));
                        child1.runAction(cc.sequence(
                            cc.delayTime(.35),
                            cc.scaleTo(.10, .5),
                            cc.callFunc(function () {
                                child1.setScale(1.1);
                            })
                        ));
                    }
                    if (child.getName() == 'card2') {
                        child2 = child;
                        child2.runAction(cc.moveBy(.15, 0, 90));
                        child2.runAction(cc.sequence(
                            cc.delayTime(.35),
                            cc.scaleTo(.10, .5),
                            cc.callFunc(function () {
                                child2.setScale(1.1);
                            })
                        ));
                    }
                    if (child.getName() == 'card3') {
                        child3 = child;
                        child3.runAction(cc.moveBy(.15, 0, 135));
                        child3.runAction(cc.sequence(
                            cc.delayTime(.35),
                            cc.scaleTo(.10, .5),
                            cc.callFunc(function () {
                                child3.setScale(1.1);
                            })
                        ));
                    }
                }
            }
        }
    },
};

var removeInArray = function (list, indexList) {
    if (list == null) return;
    indexList.sort(function (a, b) {
        return b - a;
    });

    for (var i = 0; i < indexList.length; i++) {
        list.splice(indexList[i], 1);
    }
};